package kr.co.hk.cp;

import java.util.regex.Matcher;

import android.content.ContentProvider;
import android.content.ContentValues;
import android.content.UriMatcher;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.net.Uri;

public class CPProvider extends ContentProvider {
	static final Uri CONTENT_URI 
				= Uri.parse("content://exam.Data.EnglishWord/word");
	static final int ALLWORD = 1;
	static final int ONEWORD = 2;
	static final UriMatcher matcher;
	static{
		matcher = new UriMatcher(UriMatcher.NO_MATCH);
		matcher.addURI("exam.Data.EnglishWord", "word", ALLWORD);
		matcher.addURI("exam.Data.EnglishWord", "word/*", ONEWORD);
	}
	
	SQLiteDatabase db;

	@Override
	public boolean onCreate() {
		WordDBHelper helper = new WordDBHelper(getContext());
		db = helper.getWritableDatabase();
		return true;
	}
	
	@Override
	public Cursor query(Uri uri, String[] projection, String selection,
			String[] selectionArgs, String sortOrder) {
		String sql;
		
		sql = "SELECT eng, han FROM dic ";
//		if (matcher.match(uri)==ONEWORD) {
//			sql += " where eng = '"+uri.getPathSegments().get(1)+"' ";
//		}
		
		Cursor cursor = db.rawQuery(sql, null);
		
		return cursor;
	}	
	
	@Override
	public int delete(Uri uri, String selection, String[] selectionArgs) {
		// TODO Auto-generated method stub
		return 0;
	}

	@Override
	public String getType(Uri uri) {
		if(matcher.match(uri) == ALLWORD){
			return "vnd.EnglishWord.Data.cursor.item/word";
		}
		if(matcher.match(uri) == ONEWORD){
			return "vnd.EnglishWord.Data.cursor.dir/words";
		}
		return null;
	}

	@Override
	public Uri insert(Uri uri, ContentValues values) {
		// TODO Auto-generated method stub
		return null;
	}

	@Override
	public int update(Uri uri, ContentValues values, String selection,
			String[] selectionArgs) {
		// TODO Auto-generated method stub
		return 0;
	}

}
